package servlet.generatereportservlet;

import bean.vo.AppointmentVo;
import bean.vo.MaintenanceVo;
import cn.afterturn.easypoi.entity.ImageEntity;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.generatewoord.JFreeUtil;
import commons.generatewoord.WordUtil;
import dao.EchartsDao;
import dao.impl.EchartsDaoImpl;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;

/**
 * servlet.generatereportservlet
 * User: 邱
 * Date: 2024/12/23 20:56
 * Description:
 * Version: V1.0
 */
@WebServlet("/reports/*")
public class GenerateReportsServlet extends BaseServlet {
    public File generateWord(HttpServletRequest request){
        Integer ownerNum = NumberFormat.stringToInteger(request.getParameter("ownerNum"));
        Integer dwellerNum = NumberFormat.stringToInteger(request.getParameter("dwellerNum"));
        Integer visitorNum = NumberFormat.stringToInteger(request.getParameter("visitorNum"));
        Integer saleGoodsNum = NumberFormat.stringToInteger(request.getParameter("saleGoodsNum"));
        Integer complaintNum = NumberFormat.stringToInteger(request.getParameter("complaintNum"));
        Integer hasPayment = NumberFormat.stringToInteger(request.getParameter("hasPayment"));
        Integer notPayment = NumberFormat.stringToInteger(request.getParameter("notPayment"));

        EchartsDao echartsDao = new EchartsDaoImpl();
        List<AppointmentVo> medicalList = echartsDao.findMedicalList();
        List<MaintenanceVo> MaintenanceList = echartsDao.findMaintenance();

        HashMap<String, Object> map = new HashMap<>(9);
        map.put("ownerNum", ownerNum);
        map.put("dwellerNum", dwellerNum);
        map.put("visitorNum", visitorNum);
        map.put("saleGoodsNum", saleGoodsNum);
        map.put("complaintNum", complaintNum);
        map.put("hasPayment", hasPayment);
        map.put("notPayment", notPayment);

        HashMap<String, Integer> medical = new HashMap<>(medicalList.size());
        medicalList.forEach(m -> medical.put(m.getMedicalItem(), Math.toIntExact(m.getValue())));
        ImageEntity imageMedical = JFreeUtil.pieChart("医疗预约情况", medical, 500, 300);
        map.put("medical", imageMedical);

        HashMap<String, Integer> maintenance = new HashMap<>(MaintenanceList.size());
        MaintenanceList.forEach(m -> maintenance.put(m.getMaintenanceItem(), Math.toIntExact(m.getMaintenanceNum())));
        ImageEntity imageMaintenance = JFreeUtil.pieChart("维修项目情况", maintenance, 500, 300);
        map.put("maintenance", imageMaintenance);

        String tmpPath = WordUtil.exportWord("template/demo1.docx", "D:/wordDemo/", "tempWord.docx", map);
        if (tmpPath != null){
            return new File(tmpPath);
        }
        return null;
    }
}
